﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Editor;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Properties;
using System.Drawing.Printing;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Render;
using ITAP.Core.Calculations;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationPrint : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Печать"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.Other; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.Printer.ToBitmap(); }
        }

        ModelCoordinateConverter coordinateConverter;
        DraftRender render;
        public void Execute(ModelControl modelControl)
        {
            coordinateConverter = new ModelCoordinateConverter();
            render = new DraftRender();

            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() != DialogResult.OK)
                return;

            List<ModelCell> modelsToPrint = new List<ModelCell>();
            foreach (IComponent component in modelControl.ModelContainer.InnerComponents)
                modelsToPrint.Add(component as ModelCell);
            PaperSize paperSize = printDialog.PrinterSettings.DefaultPageSettings.PaperSize;
            Graphics g = printDialog.PrinterSettings.CreateMeasurementGraphics();
            
            SizeF visiblePageSize = new SizeF(g.VisibleClipBounds.Width, g.VisibleClipBounds.Height);
            ImageExportForm form = new ImageExportForm(modelsToPrint, printDialog.PrinterSettings);

            if (form.ShowDialog() != DialogResult.OK)
                return;

            foreach (ImageExportInfo info in form.ImageExportInfo)
            {
                if (!info.Enabled)
                    continue;
                this.currentPrintingInfo = info;
                PrintDocument document = new PrintDocument();                
                document.PrinterSettings = printDialog.PrinterSettings;
                document.DefaultPageSettings = this.currentPrintingInfo.PageSettings;
                document.DocumentName = this.currentPrintingInfo.Model.Name;
                document.PrintPage += new PrintPageEventHandler(document_PrintPage);
                document.Print();
            }                        
        }

        private ImageExportInfo currentPrintingInfo;
        private 

        void document_PrintPage(object sender, PrintPageEventArgs e)
        {           
            RectangleF pageRectangle = e.Graphics.VisibleClipBounds;
//            this.currentPrintingInfo.Model.Update(this.currentPrintingInfo.DimensionsPreferences);
            RectangleF rectangleToDraw = e.Graphics.VisibleClipBounds;

//            this.currentPrintingInfo.Model.Update(this.currentPrintingInfo.DimensionsPreferences);
            RectangleF modelBoundingBox = this.currentPrintingInfo.Model.Model.BoundingBox;

            Image image = new Bitmap((int)modelBoundingBox.Width, (int)modelBoundingBox.Height);

            Graphics graphics = Graphics.FromImage(image);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphics.Clear(Color.White);
            coordinateConverter.UseAutoScale = true;
            ModelPoint center = new ModelPoint(modelBoundingBox.X + modelBoundingBox.Width / 2,
                                               modelBoundingBox.Y + modelBoundingBox.Height / 2);
            render.Initialize(graphics,
                             (int)image.Width,
                             (int)image.Height,
                             modelBoundingBox,
                             coordinateConverter,
                             center);
            render.SetSizesFont(this.currentPrintingInfo.DimensionsPreferences.Font);
            modelBoundingBox = render.GetModelBoundingBoxAfterDrawing(this.currentPrintingInfo.Model,
                                                                      image.Width, image.Height);

            center = new ModelPoint(modelBoundingBox.X + modelBoundingBox.Width / 2,
                                    modelBoundingBox.Y + modelBoundingBox.Height / 2);
            image = new Bitmap((int)modelBoundingBox.Width, (int)modelBoundingBox.Height);

            graphics = Graphics.FromImage(image);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphics.Clear(Color.White);
            render.Initialize(graphics,
                             (int)image.Width,
                             (int)image.Height,
                             modelBoundingBox,
                             coordinateConverter,
                             center);
            render.SetSizesFont(this.currentPrintingInfo.DimensionsPreferences.Font);
            render.Visit(this.currentPrintingInfo.Model);

            Graphics panelPreviewGraphics = e.Graphics;

            Rectangle rect = Rectangle.Empty;
            int width = (int)coordinateConverter.ModelMeasureToScreenMeasure(image.Width,
                                                                             (int)rectangleToDraw.Width,
                                                                             (int)rectangleToDraw.Height,
                                                                             new RectangleF(0, 0, image.Width, image.Height));
            int height = (int)coordinateConverter.ModelMeasureToScreenMeasure(image.Height,
                                                                              (int)rectangleToDraw.Width,
                                                                              (int)rectangleToDraw.Height,
                                                                              new RectangleF(0, 0, image.Width, image.Height));
            rect = new Rectangle((int)(rectangleToDraw.X + rectangleToDraw.Width / 2) - width / 2,
                                 (int)(rectangleToDraw.Y + rectangleToDraw.Height / 2) - height / 2,
                                 width,
                                 height);
            panelPreviewGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            panelPreviewGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            panelPreviewGraphics.Clear(Color.Gray);
            panelPreviewGraphics.FillRectangle(Brushes.White, rectangleToDraw);
            panelPreviewGraphics.DrawImage(image, rect,
                                            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

            panelPreviewGraphics.DrawRectangle(Pens.Black, Rectangle.Truncate(rectangleToDraw)); 
            //this.currentPrintingInfo.Model.Update(GlobalPreferences.Preferences.EditorDimensions);
        }

        public bool CanUndo
        {
            get { return false; }
        }

        #endregion
    }
}
